package com.seashop.tree;

import com.alibaba.fastjson.JSON;
import com.seashop.redis.RedisZero;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author moredln
 * @desc 金融计算实现——泰国
 * @date 2020/11/27 11:29 下午
 */
@Component("TH1.0")
public class FinancialComputingThailand implements IFinancialComputing {

    @Autowired
    private RedisZero redisZero;
    /**
     * member regulation version
     */
    private static String MEMBER_RULE_VERSION = "1.0";

    @Override
    public boolean checkDegrade(UserNodeRedis userNodeRedis, boolean isBuyer) {
        // 2->1
        if (userNodeRedis.getUserLevel() == 2) {
            return false;
        }

        // 3->2
        if (userNodeRedis.getUserLevel() == 3) {
            int upgrade2to3DirectNum = Integer.parseInt(getMemberRule("upgrade_2to3_direct").toString());
            int upgrade2to3TeamNum = Integer.parseInt(getMemberRule("upgrade_2to3_team").toString());
            return userNodeRedis.getChildrenCountByLevel(2)
                    + userNodeRedis.getChildrenCountByLevel(3)
                    + userNodeRedis.getChildrenCountByLevel(4) < upgrade2to3DirectNum
                    || userNodeRedis.getTeamCountByLevel(2)
                    + userNodeRedis.getTeamCountByLevel(3)
                    + userNodeRedis.getTeamCountByLevel(4) < upgrade2to3TeamNum;
        }
        // 4->3
        if (userNodeRedis.getUserLevel() == 4) {
            int upgrade3to4DirectNum = Integer.parseInt(getMemberRule("upgrade_3to4_direct").toString());
            return userNodeRedis.getChildrenCountByLevel(3) + userNodeRedis.getChildrenCountByLevel(4) < upgrade3to4DirectNum;
        }
        return false;
    }

    @Override
    public Object getMemberRule(String ruleKey) {
        return JSON.parse(redisZero.get(getCountryCode() + "_" + MEMBER_RULE_VERSION + "_" + ruleKey).toString());
    }

    @Override
    public String getCountryCode() {
        return "TH";
    }

}
